var api = require('../src/api/api');
var router = require('./../src/router');
var test = require('tape').test;

// test('test wrong json request', function(t) {
//     var generateMnemonic = 	"{\"version\":, \"method\": \"generateMnemonic\", \"params\": {\"length\":\"12\", \"language\":\"english\"}}";
//     var ret = router.route(generateMnemonic);
//     var retObj = JSON.parse(ret);
//     t.equal(retObj.state, 10005, " wrong json request success");
//     t.end();
// });
//
// test('test api version', function(t) {
//     var generateMnemonic = 	{"version":"1.0", "method": "generateMnemonic", "params": {"length":"12", "language":"english"}};
//     var ret = router.route(JSON.stringify(generateMnemonic));
//     var retObj = JSON.parse(ret);
//     t.equal(retObj.state, 10000, "api version success");
//     t.end();
// });
//
// test('test wrong method ', function(t) {
//     var generateMnemonic = 	{"version":"1.0", "method": "xxxxx", "params": {"length":"12", "language":"english"}};
//     var ret = router.route(JSON.stringify(generateMnemonic));
//     var retObj = JSON.parse(ret);
//     t.equal(retObj.state, 10004, "not found method");
//     t.end();
// });
//
// test('test generate english mnemonic, length 12', function(t) {
//     var generateMnemonic = 	{"version":"1.0", "method": "generateMnemonic", "params": {"length":"12", "language":"english"}};
//     var ret = api.handleMsg(generateMnemonic);
//     var retObj = JSON.parse(ret);
//     t.equal(retObj.state, 10000, "generate english mnemonic success");
//     t.end();
// });
//
// test('test generate chinese mnemonic, length 12', function(t) {
//     var generateMnemonic = 	{"version":"1.0", "method": "generateMnemonic", "params": {"length":"12", "language":"chinese_simplified"}};
//     var ret = api.handleMsg(generateMnemonic);
//     var retObj = JSON.parse(ret);
//     t.equal(retObj.state, 10000, "generate chinese mnemonic success");
//     t.end();
// });

// test('test generate address ', function(t) {
//     var symbols = ["BTc", "BTC-T", "BCH", "BtG", "LTC", "ETH", "etc", "dash", "doge", "zec", "xrp"];
//     for (var i = 0; i < symbols.length; i++) {
//         console.log("start generate %s address ...", symbols[i]);
//         var generateAddress = 	{"version":"1.0", "method": "recoverAddress", "params": {"symbol":symbols[i],  "language":"english", "mnemonic":"practice seek mad fold near shuffle wear toilet torch earn near response", "startIndex":"0", "amount":"1"}};
//         var ret = api.handleMsg(generateAddress);
//         console.error(6,"           ",ret);
//         var retObj = JSON.parse(ret);
//         console.error(7,"             ",retObj);
//         t.equal(retObj.state, 10000, "generate address success");
//     }
//     t.end();
// });



// test('test generate address ', function(t) {
//     var symbols = ["BTc"];
//     for (var i = 0; i < symbols.length; i++) {
//         console.log("start generate %s address ...", symbols[i]);
//         var generateAddress = 	{"version":"1.0", "method": "recoverAddress", "params": {"symbol":symbols[i],  "language":"english", "mnemonic":"practice seek mad fold near shuffle wear toilet torch earn near response", "startIndex":"0", "amount":"10"}};
//         var ret = api.handleMsg(generateAddress);
//         console.error(6,"           ",ret);
//         var retObj = JSON.parse(ret);
//         console.error(7,"             ",retObj.data);
//         t.equal(retObj.state, 10000, "generate address success");
//     }
//     t.end();
// });


//
// test('test signBTCTx BTC', function(t) {
//     var input = [
//         {"txid":"f74079688a26298dfa7cf1c58aec4c495993499bb14058cf52109b867ebf0ed2", "privateKey":"cUj2sYiSZYUwAD4MP5hFf6ceCVzR9n68yEW1CB2Kx5GQKTm8kcTk", "vout": 1,"satoshis":5000000},
//         {"txid":"0c0b3acaf040fb1a47ea1ea00112c5a5ea7637970154e2eeecc720d2a47ca543", "privateKey":"cRw1ubPGna7ovBaU9ZxvmvQaHq3gC396mg6MfgPotbLQ6h924r6c", "vout": 1,"satoshis":652200000},
//         {"txid":"7b20586b8b3cc8c4c851e370cd77461f3aa54166c2cfd64d61b895f56e110067", "privateKey":"cRw1ubPGna7ovBaU9ZxvmvQaHq3gC396mg6MfgPotbLQ6h924r6c", "vout": 0,"satoshis":48984155445},
//         {"txid":"ff735b05b592f176c822c8b4b2a2c7817dae109f72474cd2feed2bc0bb6f59f5", "privateKey":"cRw1ubPGna7ovBaU9ZxvmvQaHq3gC396mg6MfgPotbLQ6h924r6c", "vout": 0,"satoshis":321654645}
//         ];
//
//
//     var output = [{"address":"2NAUGMDbJT85eadUieLeARrWamMHm2A7kUo", "amount":400000000},
//         {"address":"2NDiBkcGyYc4fehfzG2KAwAkyiS4FYbhCB1", "amount":295000000}];
//     var signBTCTx = 	{"version":"1.0", "method": "signTx", "params": {"symbol": "BTC-T", "input":input, "output":output,"fee":100}};
//
//     var ret = api.handleMsg(signBTCTx);
//     console.error(ret);
//     var rightAnswer = {"state":10000,"message":"success","data":{"txid":"cb6a8eab04d4fd2a114ef1e11d0dc5885e30d6c212bdccb1e945d320db08d0fd","rawTx":"0100000004d20ebf7e869b1052cf5840b19b499359494cec8ac5f17cfa8d29268a687940f7010000006a4730440220488d077c5dbe016efba075f92b5749f191bb4eb01821154255d70e43e9c60b1c02203f736de85b81bdc35205ab5b7ab197ed4ddd359d0bf405660647e4bdf6c3eab30121023498cfb68e409c99565689d76987ab5035e8498ef816a50ef2dbb37f585dd14effffffff43a57ca4d220c7eceee25401973776eaa5c51201a01eea471afb40f0ca3a0b0c010000006a4730440220552173e368c7f72f40b35d314188f5658dc9e5e9f4d024b789f584bcdbae988f02204a69bd6d31b565519d9b77a8a7623173b00e7219a109ba00cc6578990e48cbca012102a30204513b6b533358fbc58494116dead5e7e5f41d6f322dc14c57d35e64b5bdffffffff6700116ef595b8614dd6cfc26641a53a1f4677cd70e351c8c4c83c8b6b58207b000000006b4830450221008838a1c17ee07f0eed7ed2fae0e5c38b931189da26d35bb3d0d3ee9fcefd1279022008942e6572636169f47369030e75ec5c4e067719117e982c69d9f6bb991dc70c012102a30204513b6b533358fbc58494116dead5e7e5f41d6f322dc14c57d35e64b5bdfffffffff5596fbbc02bedfed24c47729f10ae7d81c7a2b2b4c822c876f192b5055b73ff000000006b483045022100a25105a295eecc5272186a2cf4f9801381709feefee6e7b79f0822383c2b32d5022070b8ad31c458b6ac98fd8689425693aa9f0fac325a14783b768f9d70a322ccb3012102a30204513b6b533358fbc58494116dead5e7e5f41d6f322dc14c57d35e64b5bdffffffff020084d7170000000017a914bcf184420cd4c06186b1766d9d0dba5cddb1ec8a87c05795110000000017a914e07be15c32197ede8e7be8f851f59755646cb5318700000000"}};
//     t.equal(ret, JSON.stringify(rightAnswer), "signBTCTx BTC success");
//     t.end();
// });


test('test countFee BTC',   function(t) {
    var input = [
        {"txid":"f74079688a26298dfa7cf1c58aec4c495993499bb14058cf52109b867ebf0ed2", "privateKey":"cUj2sYiSZYUwAD4MP5hFf6ceCVzR9n68yEW1CB2Kx5GQKTm8kcTk", "vout": 1,"satoshis":5000000},
        {"txid":"0c0b3acaf040fb1a47ea1ea00112c5a5ea7637970154e2eeecc720d2a47ca543", "privateKey":"cRw1ubPGna7ovBaU9ZxvmvQaHq3gC396mg6MfgPotbLQ6h924r6c", "vout": 1,"satoshis":652200000},
        {"txid":"7b20586b8b3cc8c4c851e370cd77461f3aa54166c2cfd64d61b895f56e110067", "privateKey":"cRw1ubPGna7ovBaU9ZxvmvQaHq3gC396mg6MfgPotbLQ6h924r6c", "vout": 0,"satoshis":48984155445},
        {"txid":"ff735b05b592f176c822c8b4b2a2c7817dae109f72474cd2feed2bc0bb6f59f5", "privateKey":"cRw1ubPGna7ovBaU9ZxvmvQaHq3gC396mg6MfgPotbLQ6h924r6c", "vout": 0,"satoshis":321654645}
    ];

    var output = [{"address":"2NAUGMDbJT85eadUieLeARrWamMHm2A7kUo", "amount":400000000},
        {"address":"2NDiBkcGyYc4fehfzG2KAwAkyiS4FYbhCB1", "amount":295000000}];
    var countFee = 	{"version":"1.0", "method": "countFee","fee":1000,"type":"all", "params": {"symbol": "BTC-T", "input":input, "output":output,"fee":100}};

   let ret = api.handleMsg(countFee);
    console.log(ret);
    var rightAnswer={"status":10000,"message":"success","data":{"feeCost":70400}};
    t.equal(null, JSON.stringify(rightAnswer), "countFee BTC success");
    t.end();
});


//
// test('test signETHTx ETH', function(t) {
//     var tmp = {
//         "from":"0x8C39e3540F626Ce753F2EAE0bBE643aB569b297a",
//         "to":"0xea3300ca2e51b69026939b07fba329a639f50770",
//         "value": "0x8AC7230489E80000",
//         "nonce": "0x3",
//         "gasPrice": "0x4a817c800",
//         "gasLimit":"0x15f90"
//     };
//     var signETHTx = 	{"version":"1.0", "method": "signTx", "params": {"symbol": "ETH", "privateKey":"de06e2f52440dd254c9b5fb00f036a434d447795912e2feaac8ef75f1a8884e9", "txData":tmp}};
//     var ret = api.handleMsg(signETHTx);
//     var rightAnswer = {"state":10000,"message":"success","data":{"txid":"0x0e899c84ea53e2c8ba3ea60d38eb4687d23765fd1678e144445bc96799a3932f","rawTx":"0xf86d038504a817c80083015f9094ea3300ca2e51b69026939b07fba329a639f50770888ac7230489e80000801ca0cfb1e88c29eae6e8b6a272cf532c6f565dfa4312e261ba97d487354ddf3a5b2ba04fdc0cbe906bc8f12bb44aa1ed0faa60079040399a36adf1afb48f784cf99eaa"}};
//     t.equal(ret, JSON.stringify(rightAnswer), "signETHTx success");
//     t.end();
// });
//
// test('test 1 to 1 signLTCTx LTC', function(t) {
//     var input = [{"txid":"b81517c0553d0aab41fd3ded54507506b12f5527698bb733aa373c1d1ef8e69a", "outputIndex": 0, "script":"76a914801d162ab176f7963fa46170417941f4d45c314788ac", "satoshis":10100000}];
//     var output = [{"address":"LXaoPDpLNmG5ZG3YZn5asht6VLURFQKcRe", "satoshis":10000000}];
//
//     var privateKey = ["T5s3b7H3GdDyUEFhG1nyRHqCUf7mNd9rwF1M8P26Qjd6RanZCzp1"];
//     var signLTCTx = 	{"version":"1.0", "method": "signTx", "params": {"symbol": "LTC", "privateKey":privateKey, "input":input, "output":output}};
//
//     var ret = api.handleMsg(signLTCTx);
//     var rightAnswer = {"state":10000,"message":"success","data":{"txid":"b8c61b1f6670551d05297844937e8e4c5e240e09effb59b3f449d8ca126cb8e0", "rawTx":"01000000019ae6f81e1d3c37aa33b78b6927552fb106755054ed3dfd41ab0a3d55c01715b80000000000ffffffff0180969800000000001976a9148792d528d23d80e41b61c95315d3d5d88dafedb488ac00000000"}};
//     t.equal(ret, JSON.stringify(rightAnswer), "signLTCTx BTC success");
//     t.end();
// });
//
//
// test('test 1 to 2 signLTCTx LTC', function(t) {
//     var input = [{"txid":"d4eff1f54cd48cb3932a8995390c6cea9066d4d447b77d0926fc5064a401cc63", "address":"LWuMbjp37id6YspS2RxAKW4QX4wVQ6R7nW", "outputIndex": 0, "script":"76a914801d162ab176f7963fa46170417941f4d45c314788ac", "satoshis":9900000}];
//     var output = [{"address":"MFWBZo3NbbVDrQisaT1BpKK3MTjn2FW6HW", "satoshis":9000000},
//                 {"address":"LWuMbjp37id6YspS2RxAKW4QX4wVQ6R7nW", "satoshis":800000}];
//
//     var privateKey = ["T6T5FZuSh2YYSsShnCadEbBWT5pbYCT39EPD7wE6z97t6upq5iQw"];
//     var signLTCTx = 	{"version":"1.0", "method": "signTx", "params": {"symbol": "LTC", "privateKey":privateKey, "input":input, "output":output}};
//
//     var ret = api.handleMsg(signLTCTx);
//     var rightAnswer = {"state":10000,"message":"success","data":{"txid":"f2819732d6fd2ea4b3efb9cb2dabadbb8ccc3ebf08ab4f89d82929454638f96f", "rawTx":"010000000163cc01a46450fc26097db747d4d46690ea6c0c3995892a93b38cd44cf5f1efd4000000006b483045022100a6bf8dc11d5aca09e967ccaa860d7d9cd9aa1d3cc3bde254614bbdf617a50e1102206d37b21eaf9ba097f0c5416b506d2ed1c59720bb8b2dffad1cfbd4f7038a2643012102c3a9c9faec1dfa5dedf6aa966c388c01c1ee661f95ff35807c92691e92933fd1ffffffff02405489000000000017a91453688d77acc65f4f23654fd527c4a40fdea8c73a8700350c00000000001976a914801d162ab176f7963fa46170417941f4d45c314788ac00000000"}};
//     t.equal(ret, JSON.stringify(rightAnswer), "signLTCTx 1 to 2 success");
//     t.end();
// });
//
// test('test 2 to 2 signLTCTx LTC', function(t) {
//     var input = [{"txid":"f2819732d6fd2ea4b3efb9cb2dabadbb8ccc3ebf08ab4f89d82929454638f96f", "address":"LWuMbjp37id6YspS2RxAKW4QX4wVQ6R7nW", "outputIndex": 1, "script":"76a914801d162ab176f7963fa46170417941f4d45c314788ac", "satoshis":800000},
//         {"txid":"da3b5def91d7acf4d167d9291bf21437451eb3d9a8508f1ff6c215d63a326caa", "address":"LXaoPDpLNmG5ZG3YZn5asht6VLURFQKcRe", "outputIndex": 1, "script":"76a9148792d528d23d80e41b61c95315d3d5d88dafedb488ac", "satoshis":5000000}];
//     var output = [{"address":"LXaoPDpLNmG5ZG3YZn5asht6VLURFQKcRe", "satoshis":700000},
//                 {"address":"LWuMbjp37id6YspS2RxAKW4QX4wVQ6R7nW", "satoshis":5000000}];
//
//     var privateKey = ["T6T5FZuSh2YYSsShnCadEbBWT5pbYCT39EPD7wE6z97t6upq5iQw", "T7N2NvwixNsKc6VDtfdCt2fvEJoKZr1FDMvcyPcX7gdQc4H2iFT7"];
//     var signLTCTx = 	{"version":"1.0", "method": "signTx", "params": {"symbol": "LTC", "privateKey":privateKey, "input":input, "output":output}};
//
//     var ret = api.handleMsg(signLTCTx);
//     var rightAnswer = {"state":10000,"message":"success","data":{"txid":"6b7a3256f1657ae2e2716fdce5f3f96170f62505410a8a36c457d80123034d32", "rawTx":"01000000026ff93846452929d8894fab08bf3ecc8cbbadab2dcbb9efb3a42efdd6329781f2010000006a47304402206413ca08c6a11bd2d3898b047c5e70e4f6767c76abaffe139bd582c95acb5b6502204def0fc39e5778f546bf58cbe6dfbb368dde1de52a92459ff5a339f66830e2b2012102c3a9c9faec1dfa5dedf6aa966c388c01c1ee661f95ff35807c92691e92933fd1ffffffffaa6c323ad615c2f61f8f50a8d9b31e453714f21b29d967d1f4acd791ef5d3bda010000006a4730440220023a8a42da7944f8e37f7885d12b74a9778cd0127a5754e44a4ffeeb0e27e317022031e856975f4b46b3365ac361d41ca896e6dfa77c203d4bcca44a378f599eed0b012103dc6d70f4f213c59dfb98b62cb63a9027b13524b84914ccd21112f88afa086471ffffffff0260ae0a00000000001976a9148792d528d23d80e41b61c95315d3d5d88dafedb488ac404b4c00000000001976a914801d162ab176f7963fa46170417941f4d45c314788ac00000000"}};
//     t.equal(ret, JSON.stringify(rightAnswer), "signLTCTx 2 to 2 success");
//     t.end();
// });
//
// test('test 2 to 1 signLTCTx LTC', function(t) {
//     var input = [{"txid":"6b7a3256f1657ae2e2716fdce5f3f96170f62505410a8a36c457d80123034d32", "address":"LXaoPDpLNmG5ZG3YZn5asht6VLURFQKcRe", "outputIndex": 0, "script":"76a9148792d528d23d80e41b61c95315d3d5d88dafedb488ac", "satoshis":700000},
//         {"txid":"6b7a3256f1657ae2e2716fdce5f3f96170f62505410a8a36c457d80123034d32", "address":"LWuMbjp37id6YspS2RxAKW4QX4wVQ6R7nW", "outputIndex": 1, "script":"76a914801d162ab176f7963fa46170417941f4d45c314788ac", "satoshis":5000000}];
//     var output = [{"address":"MFWBZo3NbbVDrQisaT1BpKK3MTjn2FW6HW", "satoshis":5600000}];
//
//     var privateKey = ["T6T5FZuSh2YYSsShnCadEbBWT5pbYCT39EPD7wE6z97t6upq5iQw", "T7N2NvwixNsKc6VDtfdCt2fvEJoKZr1FDMvcyPcX7gdQc4H2iFT7"];
//     var signLTCTx = 	{"version":"1.0", "method": "signTx", "params": {"symbol": "LTC", "privateKey":privateKey, "input":input, "output":output}};
//
//     var ret = api.handleMsg(signLTCTx);
//     var rightAnswer = {"state":10000,"message":"success","data":{"txid":"32ba267bb4e2d3eb90024379bf7543e52efc16e094752d9a380907ad0d3a2fb3", "rawTx":"0100000002324d032301d857c4368a0a410525f67061f9f3e5dc6f71e2e27a65f156327a6b000000006b483045022100ea94d4f9d10cac165fe873dc3523e901a79ecfe59ac68ad8441126327ba73e6802204f43f898f69af76e770c72d06db182cd8e4b35b856a75a3057488e49d42c52d7012103dc6d70f4f213c59dfb98b62cb63a9027b13524b84914ccd21112f88afa086471ffffffff324d032301d857c4368a0a410525f67061f9f3e5dc6f71e2e27a65f156327a6b010000006b483045022100a795372491fef83ab0803589b122635af708691c057f4b7601d181f7caa93ccb022051dfa13d3fafeb8bc239d2a0220bd5555817c9eb61fac15f1b21ebd198581f3d012102c3a9c9faec1dfa5dedf6aa966c388c01c1ee661f95ff35807c92691e92933fd1ffffffff01007355000000000017a91453688d77acc65f4f23654fd527c4a40fdea8c73a8700000000"}};
//     t.equal(ret, JSON.stringify(rightAnswer), "signLTCTx 2 to 2 success");
//     t.end();
// });
//
//
// test('test LTC signLTCMsg', function(t) {
//     var signLTCTx = {"version":"1.0", "method": "signMsg", "params": {"symbol":"LTC", "message": "This is an example of a signed message", "privateKey":"T6T5FZuSh2YYSsShnCadEbBWT5pbYCT39EPD7wE6z97t6upq5iQw"}};
//
//     var ret = api.handleMsg(signLTCTx);
//     var retObj = JSON.parse(ret);
//     t.equal(retObj.state, 10000, "signMsg LTC retrue ok");
//     t.end();
// });
//
// test('test LTC verifyLTCMsg', function(t) {
//     var verifyLTCTx = {"version":"1.0", "method": "verifyMsg", "params": {"symbol":"LTC", "message":"This is an example of a signed message", "address": "LWuMbjp37id6YspS2RxAKW4QX4wVQ6R7nW", "signature":"H9DCctekNbmRQetnFG3aOtGLQFR9dz078JEuqZksMsUTPbGb/p9pIcKDfgx/D5LKfvsUsGgyz7Unyc7QIyqlBQw="}};
//
//     var ret = api.handleMsg(verifyLTCTx);
//     var retObj = JSON.parse(ret);
//     t.equal(retObj.state, 10000, "verifyMsg LTC retrue ok");
//     t.end();
// });
//
// test('test BTC signBTCMsg', function(t) {
//     var signBTCTx = {"version":"1.0", "method": "signMsg", "params": {"symbol":"BTC", "message": "This is an example of a signed message", "privateKey":"L4hArLJKVnJtX1NJpjC3yoNGwVFvw8y3es5Cy9b5Y4DoTy3FgJjW"}};
//
//     var ret = api.handleMsg(signBTCTx);
//     var rightAnswer = {"state":10000,"message":"success","data":{"signMsg":"H6FJxxtlyN/aC+NVTa2RTw0T7MShqjMA9NHYoLmpDV8PZJ06qhpMoHQor0QbC3iZ+aLTDWBpSF/49zQsz1UW1bM="}};
//     t.equal(ret, JSON.stringify(rightAnswer), "signMsg BTC retrue ok");
//     t.end();
// });
//
// test('test BTC verifyLTCMsg', function(t) {
//     var verifyBTCTx = {"version":"1.0", "method": "verifyMsg", "params": {"symbol":"BTC", "message":"This is an example of a signed message", "address": "1EgHNyfE2Z1u3AqdSkNXnRgkgLxKDNSrMv", "signature":"H6FJxxtlyN/aC+NVTa2RTw0T7MShqjMA9NHYoLmpDV8PZJ06qhpMoHQor0QbC3iZ+aLTDWBpSF/49zQsz1UW1bM="}};
//
//     var ret = api.handleMsg(verifyBTCTx);
//     var retObj = JSON.parse(ret);
//     t.equal(retObj.state, 10000, "verifyMsg BTC retrue ok");
//     t.end();
// });
